<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="流程名称" align="center" prop="processName" />
      <el-table-column
        label="开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发起人" align="center" prop="createBy" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button type="text" @click="handleReview(scope.row)"
            >审核</el-button
          >
          <el-button type="text" @click="handleBack(scope.row)">回退</el-button>
          <el-button type="text" @click="handleStop(scope.row)">终止</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 展示弹出层 -->
    <div>
      <reviewDialog
        v-model="reviewVisible"
        :formId="formId"
        @refresh="handleQuery"
      ></reviewDialog>
    </div>
  </div>
</template>

<script setup name="Order">
import {
  waitToDoListOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/platform/order";

import reviewDialog from "@/views/workOrder/waitingToDo/dialog/reviewDialog.vue";

const { proxy } = getCurrentInstance();

const orderList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const reviewVisible = ref(false);
const formId = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskName: null,
    startTime: null,
    endTime: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询异动计划补录列表 */
function getList() {
  loading.value = true;
  waitToDoListOrder(queryParams.value).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 审核按钮操作 */
function handleReview(row) {
  reviewVisible.value = true;
  formId.value = row.id;
}

/** 回退按钮操作 */
function handleBack(row) {
  row.status = 0;
  handleUpdate(row);
}

/** 修改按钮操作 */
function handleUpdate(row) {
  updateOrder(row).then((response) => {
    proxy.$modal.msgSuccess("回退成功");
    getList();
  });
}

/** 终止按钮操作 */
function handleStop(row) {
  row.status = 0
  updateOrder(row).then((response) => {
    proxy.$modal.msgSuccess("终止成功");
    getList();
  });
}

getList();
</script>
